package com.joycessh.domain;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

/**
 * http请求返回的最外层对象
 * REST请求将返回统一的JSON响应结构，包括状态码，提示信息，以及业务数据
 * Created by Joycessh
 * 9/24/18 8:26 PM
 */
public class Result implements Serializable {

	/** 状态码. */
	private Integer code;

	/** 提示信息. */
	private String msg;

	/** 业务数据. */
	private Map<String, Object> data = new HashMap<String, Object>();

    public Result(ResultCode resultCode) {
        setResultCode(resultCode);
    }

    public Result(ResultCode resultCode,Map data) {
        setResultCode(resultCode);
        this.data = data;
    }

    public static Result success() {
        return new Result(ResultCode.SUCCESS);
    }

    public static Result success(Map data) {
        return new Result(ResultCode.SUCCESS, data);
    }

    public static Result success(ResultCode resultCode) {
        return success(resultCode, null);
    }

    public static Result success(ResultCode resultCode, Map data) {
        return new Result(resultCode, data);
    }

    public static Result failure(ResultCode resultCode) {
        return failure(resultCode, null);
    }

    public static Result failure(ResultCode resultCode, Map data) {
        return new Result(resultCode, data);
    }

	public Integer getCode() {
		return code;
	}

	public void setCode(Integer code) {
		this.code = code;
	}

	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

	public Map<String, Object> getData() {
		return data;
	}

	public void setData(Map<String, Object> data) {
		this.data = data;
	}

	public Result addData(String key,Object object) {
		this.data.put(key,object);
		return this;
	}

	public void setResultCode(ResultCode resultCode) {
		this.code = resultCode.getCode();
		this.msg = resultCode.getMsg();
	}
}
